﻿using MySql.Data.MySqlClient;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Edu;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal
{
    public partial class PartyVideoOnlineMore : UseMorePageMasterPage
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["PageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }
        private string DataCount
        {
            get
            {
                return this.Request["DataCount"] ?? "";
            }
        }

        private string PageCategory
        {
            get { return this.Request["PageCategory"] ?? ""; }
        }
        private string PageTitle
        {
            get { return this.Request["PageTitle"] ?? ""; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ControlBusiness.ControlBindDataDictionary(ddlCategory, SysDataDictionary.EduThemeCategory, true);
            this.ddlCategory.SelectedValue = this.PageCategory;
            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
                return;
            }
            else if (!string.IsNullOrEmpty(this.DataCount))
            {
                WriteDataCount();
                return;

            }
            if (!IsPostBack)
            {
                LoadData();
            }

        }
        public void LoadData()
        {
            this.hfCategory.Value = this.Category;
        }

        private void WriteList()
        {
            StringBuilder sbHtml = new StringBuilder();

            DataTable dt = GetVideoOnlineList(Convert.ToInt32(this.PageIndex), Convert.ToInt32(PageSize), PageCategory, this.Category, PageTitle, "4").Tables[0];
            dt.Columns.Add("ImgUrl", typeof(string));
            dt.Columns.Add("CreateTime2", typeof(string));
            foreach (DataRow dr in dt.Rows)
            {
                dr["CreateTime2"] = Convert.ToDateTime(dr["endTime"]).ToString("yyyy-MM-dd HH:mm");
                if (dr["fileid"].ToString() == "3")
                {
                    dr["ImgUrl"] = UpFileBusiness.GetFileUrl("a8e7ee31e8f24a0ca5e66a2d5890bb43");
                }
                else {
                 dr["ImgUrl"] = "";
                }

            }
            var json = JsonConvert.SerializeObject(dt);

            Response.Clear();
            Response.Write(json);
            Response.End();
        }
        private void WriteDataCount()
        {
            Response.Clear();
            Response.Write(GetVideoOnlineCount());
            Response.End();
        }
        #region 模块菜单
        /// <summary>
        /// 初始化module  如果通过url传参可不设置
        /// </summary>
        protected override void InitModule()
        {
            Module = "OnlineVideo";
            //Category = "";

        }
        #endregion


        private string GetVideoOnlineCount()
        {
            string connectionstr = ConfigurationManager.ConnectionStrings["AppBoxContext2"].ConnectionString; ;
            string sql = "select count(*) from t_recordfileinfo";
            return MySqlHelper.ExecuteDataset(connectionstr, sql, null).Tables[0].Rows[0][0].ToString();
        }

        private DataSet GetVideoOnlineList(int pageIndex, int pageSize, string category, string courseType, string searchKey = "", string status = "")
        {
            string connectionstr = ConfigurationManager.ConnectionStrings["AppBoxContext2"].ConnectionString; ;
            string sql = @"select SQL_CALC_FOUND_ROWS * from t_recordfileinfo
                        order by endtime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();";
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "Begin",Value = (pageIndex)*pageSize},
                new MySqlParameter(){DbType = DbType.Int32,ParameterName = "PageSize",Value = pageSize}
            };
            return MySqlHelper.ExecuteDataset(connectionstr, sql, parameters.ToArray());
        }
    }
}